package com.jsle.financial.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jsle.common.dao.mybatis.BaseDao;
import com.jsle.financial.domain.Resources;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.PageAndParameters;
import com.jsle.financial.vo.Result;


@SuppressWarnings("unchecked")
public class ResourcesDaoImpl extends BaseDao implements ResourcesDao{
	private Log log = LogFactory.getLog(ResourcesDaoImpl.class);
	/**
	 * 新增一用户
	 * @param Resource
	 * @return
	 */
	public Result addResource(Resources resources){
		Result result = new Result();
		try {
			boolean ret = insert("Resources.insertResources", resources);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "Resource"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "Resource"));
			log.error("new Resource error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一资源
	 * @param Resource
	 * @return
	 */
	public Result delResources(Resources resources){
		Result result = new Result();
		try {
			boolean ret = delete("Resources.delResourcess",resources);
			boolean ret1 = delete("Resources.delCompanyResourcess",resources);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "Resource"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "Resource"));
			log.error("del Resource error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一资源信息
	 * @param Resource
	 * @return
	 */
	public Result updateResource(Resources resources){
		Result result = new Result();
		try {
			boolean ret = update("Resources.updateResource",resources);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "Resource"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "Resource"));
			log.error("update Resource error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 根据资源代码获取一资源信息
	 * @param loginName
	 * @return
	 */
	public Result queryResourceByResourceCode(Resources resourceParameter){
		Result result = new Result();
		try {
			Resources Resource = (Resources)queryCountForObject("Resources.selResource",resourceParameter);
			if(Resource!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(Resource);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "Resource"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "Resource"));
			log.error("query Resource error## "+ e.getMessage());
		}
		return result;
	}
	
	public Result queryResourceByPage(Page page, Resources resource) {
		Result result = new Result();
		PageAndParameters param = new PageAndParameters(page.getPageIndex(),page.getPageSize(),resource.getResourceCode());
		try {
			List<Resources> res = queryForList("Resources.selResources", param);
			
			int totalCount = (Integer)queryCountForObject("Resources.selResourcesCounts", resource);
			if (totalCount < 1){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(new Page());
				return result;
			}
				

			int startIndex = Page.getStartOfPage(page.getPageIndex(), page.getPageSize());
			
			if(res!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(new Page(startIndex, totalCount, page.getPageSize(), res));
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UsersByPage"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UsersByPage"));
			log.error("query users by page error## "+ e.getMessage());
		}
		return result;
	}
	
	public List<Resources> selResourcesByPage(Page page){
		return null;
	}
}
